//
//  ShadowExample.swift
//  SwiftCode
//
//  Created by 于虎拴 on 2022/10/21.
//

import Foundation
import UIKit

class ShadowExample: ListViewController {
  override func getItemCount() -> Int {
    data.count
  }
  
  override func getItem(_ indexPath: IndexPath) -> Item {
    data[indexPath.row]
  }
  
  private lazy var data: [Item] = [
    .init(title: "backgroundColor = .red", description: "必须设置背景色", action: { [weak self] in self?.backgroundColor() }),
    .init(title: "shadowOpacity = 0.5", description: "默认值为 0.0", action: { [weak self] in self?.shadowOpacity() }),
    .init(title: "shadowOffset = (3.0, 3.0)", description: "默认值为 (0.0, -3.0)", action: { [weak self] in self?.shadowOffset() }),
    .init(title: "shadowRadius = 10.0", description: "默认值为 3.0", action: { [weak self] in self?.shadowRadius() }),
    .init(title: "cornerRadius = 25", description: "圆角阴影", action: { [weak self] in self?.cornerRadius() }),
    .init(title: "masksToBounds = true", description: "直接设置会将阴影切除", action: { [weak self] in self?.masksToBounds() }),
    .init(title: "masksToBounds && Nested", description: "嵌套方式解决 masksToBounds", action: { [weak self] in self?.masksToBoundsNested() }),
  ]
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    view.addSubview(shadowView)
    shadowView.snp.makeConstraints { make in
      make.size.equalTo(100)
      make.centerX.equalToSuperview()
      make.bottom.equalTo(view.safeAreaLayoutGuide).offset(-40)
    }
  }
  
  private lazy var shadowView = UIView()
  private lazy var innerView = UIView()
  
  private func backgroundColor() {
    if shadowView.backgroundColor == nil {
      shadowView.backgroundColor = .red
    } else {
      shadowView.backgroundColor = nil
    }
  }
  
  private func shadowOpacity() {
    // 图层阴影的不透明度，默认值为 0.0。
    shadowView.layer.shadowOpacity = shadowView.layer.shadowOpacity == 0.0 ? 0.5 : 0.0
  }
  
  private func shadowOffset() {
    let defaultValue = CGSize(width: 0.0, height: -3.0)
    //  图层阴影的偏移量，默认值为 (0.0, -3.0)
    if shadowView.layer.shadowOffset == defaultValue {
      shadowView.layer.shadowOffset = CGSize(width: 3.0, height: 3.0)
    } else {
      shadowView.layer.shadowOffset = defaultValue
    }
  }
  
  private func shadowRadius() {
    // 用于渲染图层阴影的模糊半径。默认值为 3.0
    shadowView.layer.shadowRadius = shadowView.layer.shadowRadius == 3.0 ? 10.0 : 3.0
  }
  
  private func cornerRadius() {
    // 为图层背景绘制圆角时使用的半径。默认值为 0.0
    shadowView.layer.cornerRadius = shadowView.layer.cornerRadius == 0.0 ? 25.0 : 0.0
  }
  
  private func masksToBounds() {
    // 一个布尔值，指示子层是否被裁剪到层的边界。
    shadowView.layer.masksToBounds.toggle()
  }
  
  private func masksToBoundsNested() {
    // 该属性为true时，会将边框之外的阴影切除。
    if shadowView.layer.masksToBounds {
      shadowView.addSubview(innerView)
      innerView.snp.makeConstraints { make in
        make.edges.equalToSuperview()
      }
      
      innerView.layer.masksToBounds = shadowView.layer.masksToBounds
      innerView.layer.cornerRadius = shadowView.layer.cornerRadius
      innerView.layer.maskedCorners = shadowView.layer.maskedCorners
      
      shadowView.layer.masksToBounds = false
    } else {
      innerView.removeFromSuperview()
    }
  }
}
